//
//  SEGWindmillProperty.m
//  ExcellentHome
//
//  Created by mac on 2017/12/14.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGWindmillProperty.h"
#import "SEGSEGBasicKitLayoutDefine.h"
#define kRefreshbgViewScale   (75.0/16.0)
//箭头颜色
#define kARrowColor  [UIColor colorWithRed:254 / 255.0f green:195 / 255.0f blue:45 / 255.0f alpha:1.0f]
@interface SEGWindmillProperty ()
//旋转动画
@property(nonatomic,strong)CABasicAnimation * rotationAnimation;
//刷新标签
@property(nonatomic,strong) UILabel * labelForRefresh;//下拉刷新文字

@end

@implementation SEGWindmillProperty
{
    CAShapeLayer * layer;
    CAShapeLayer * layerArrow;
    UIView * viewAnimation;
    
    CAShapeLayer * layer2;
    UIView * viewAnimation2;

    
   
}

+(instancetype)sharedWindmillVar{
    static SEGWindmillProperty * _instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

-(UIImageView *)refreshbgView{
    if (!_refreshbgView) {
        _refreshbgView = [[UIImageView alloc]init];
        _refreshbgView.backgroundColor = [UIColor clearColor];
        [_refreshbgView sizeToFit];
        _refreshbgView.frame=CGRectMake(0, -ScreenWidth/kRefreshbgViewScale,
                                        ScreenWidth,
                                        ScreenWidth/kRefreshbgViewScale);
        [_refreshbgView addSubview:self.labelForRefresh];
        
        viewAnimation = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 24, 24)];
        viewAnimation.center = CGPointMake(self.labelForRefresh.frame.origin.x-viewAnimation.frame.size.width/2.0-10, self.labelForRefresh.center.y);
        viewAnimation.clipsToBounds = NO;
        CGFloat radius = viewAnimation.frame.size.width/2.0;
        CGFloat startAngle = -M_PI/2.0;
        CGFloat endAngle = M_PI*1.5;
        UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius startAngle:startAngle endAngle:endAngle clockwise:true];
        layer = [[CAShapeLayer alloc] init];
        layer.path = path.CGPath;
        layer.backgroundColor = [UIColor clearColor].CGColor;
        layer.fillColor = [UIColor clearColor].CGColor;
        layer.strokeColor = kColor_3.CGColor;
        layer.lineWidth = 1.0;
        [viewAnimation.layer addSublayer:layer];
        layer.strokeStart = 0.0;
        layer.strokeEnd = 0.0;
        
        UIBezierPath * pathArrow = [UIBezierPath bezierPath];
        pathArrow.lineWidth = 1.0;
        [pathArrow moveToPoint:CGPointMake(radius, 5)];
        [pathArrow addLineToPoint:CGPointMake(radius, viewAnimation.frame.size.height-3*2)];
        [pathArrow stroke];
        [pathArrow moveToPoint:CGPointMake(radius-6, viewAnimation.frame.size.height-3*2-5)];
        [pathArrow addLineToPoint:CGPointMake(radius, viewAnimation.frame.size.height-3*2)];
        [pathArrow addLineToPoint:CGPointMake(radius+6, viewAnimation.frame.size.height-3*2-5)];
        [pathArrow stroke];
        layerArrow = [[CAShapeLayer alloc] init];
        layerArrow.path = pathArrow.CGPath;
        layerArrow.backgroundColor = [UIColor clearColor].CGColor;
        layerArrow.fillColor = [UIColor clearColor].CGColor;
        layerArrow.strokeColor = kARrowColor.CGColor;
        layerArrow.lineWidth = 1.0;
        [viewAnimation.layer addSublayer:layerArrow];
        
        [self.refreshbgView addSubview:viewAnimation];
        
        viewAnimation2 = [[UIView alloc] initWithFrame:viewAnimation.frame];
        viewAnimation2.clipsToBounds = NO;
        CGFloat radius2 = viewAnimation2.frame.size.width/2.0;
        CGFloat startAngle2 = -M_PI*0.3;
        CGFloat endAngle2 = M_PI*1.5;
        UIBezierPath * path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius2, radius2) radius:radius2 startAngle:startAngle2 endAngle:endAngle2 clockwise:true];
        layer2 = [[CAShapeLayer alloc] init];
        layer2.path = path2.CGPath;
        layer2.backgroundColor = [UIColor clearColor].CGColor;
        layer2.fillColor = [UIColor clearColor].CGColor;
        layer2.strokeColor = kColor_3.CGColor;
        layer2.lineWidth = 1.0;
        [viewAnimation2.layer addSublayer:layer2];
        [self.refreshbgView addSubview:viewAnimation2];
        viewAnimation2.hidden = YES;
    }
    return _refreshbgView;
}
-(void)animationBeforeRefesh:(CGFloat)contentY
{
    viewAnimation2.hidden = YES;
    [viewAnimation2.layer removeAllAnimations];
    
    viewAnimation.hidden = NO;
    CGFloat value = contentY<0?-contentY:contentY;
    CGFloat toValue = value/self.refreshbgView.frame.size.height;
    if (toValue < 0) {
        toValue = 0;
    }
    if (toValue > 1) {
        toValue = 1;
    }
    
    if (toValue==1) {
       self.labelForRefresh.text = @"释放即可刷新";
    }else{
       self.labelForRefresh.text = @"下拉即可刷新";
    }
    layer.strokeEnd = toValue*toValue*toValue*toValue*toValue*toValue;//成次方指数加速度
}
-(void)startLoadData
{
    self.labelForRefresh.text = @"正在刷新...";
    viewAnimation.hidden = YES;
    layer.strokeEnd = 0.0;
    viewAnimation2.hidden = NO;
    [viewAnimation2.layer addAnimation:self.rotationAnimation forKey:@""];
}
-(UIImageView *)pullRefreshViews{
    if (!_pullRefreshViews) {
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSInteger i = 1; i <= 12; i++) {
           // NSString *imgName = [NSString stringWithFormat:@"pull_refresh_%02zd",i];
            NSString *path = [[[NSBundle mainBundle] pathForResource:@"SEGResources" ofType:@"bundle"] stringByAppendingPathComponent:[NSString stringWithFormat:@"pull_refresh_%02zd",i]];
          //  NSURL* url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
         //   [arrM addObject:[UIImage imageWithContentsOfFile:SEGPicName(imgName)]];
            [arrM addObject:[UIImage imageWithContentsOfFile:path]];
        }
        _pullRefreshViews = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pull_refresh_01")]];
        _pullRefreshViews.animationImages = arrM;
        _pullRefreshViews.animationDuration = 1.0;
        [_pullRefreshViews startAnimating];
    }
    return _pullRefreshViews;
}

-(UILabel *)pullRefreshLabel{
    if (!_pullRefreshLabel) {
        _pullRefreshLabel = [[UILabel alloc]init];
        _pullRefreshLabel.text = @"松开后刷新";
        _pullRefreshLabel.font = [UIFont systemFontOfSize:11];
        _pullRefreshLabel.textColor = kColor_3;
    }
    return _pullRefreshLabel;
}
-(CABasicAnimation*)rotationAnimation{
    if (!_rotationAnimation) {
        _rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        _rotationAnimation.toValue = @(M_PI*2.0);
        _rotationAnimation.duration = 0.5;
        _rotationAnimation.repeatCount = HUGE_VALF;
    }
    return _rotationAnimation;
}
-(UILabel*)labelForRefresh{
    if (!_labelForRefresh) {
        CGRect  frameCgrect  = CGRectMake(0, -ScreenWidth/kRefreshbgViewScale,
                                           ScreenWidth,
                                           ScreenWidth/kRefreshbgViewScale);
        
        _labelForRefresh = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2.0-20, frameCgrect.size.height/2.0-10, ScreenWidth/2.0, 20)];
        _labelForRefresh.font =[UIFont boldSystemFontOfSize:11];
        _labelForRefresh.textColor = kColor_3;
    }
    return _labelForRefresh;
}

@end
